// pages/shopenter/shopenter.js

const app = getApp()

const config = require('../../config/config.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    tel: '',
    authCode: '',
    isOpenPanel: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    }

    this.getTel()
  },

  getTel: function() {
    let that = this
    wx.request({
      url: `${config.requestBaseUrl}/Index/getTel`,
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // console.log(res)
        that.setData({
          tel: res.data
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  inputAuthCode: function (e) {
    this.setData({
      authCode: e.detail.value
    })
  },

  openPanel: function() {
    this.setData({
      isOpenPanel: !this.data.isOpenPanel
    })
  },

  clickEnterBtn: function () {
    let that = this
    if (this.data.authCode == '') {
      this.showTipModel("请输入授权码")
      return
    }
    
    wx.request({
      url: `${config.requestBaseUrl}/Index/verifyAuthCode`,
      method: 'POST',
      data: {
        auth_code: this.data.authCode
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 0) {
          that.showTipModel(res.data.tip)
        } else {
          wx.navigateTo({
            url: `../shopform/shopform?shop_id=0&auth_code=${that.data.authCode}`,
          })
        }
      }
    })
   
  },


  showTipModel: function(tip) {
    wx.showModal({
      content: tip,
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          // console.log('用户点击确定')
        }
      }
    })
  }



})